package com.cqrcb.cloud.entity.api.res;

import io.swagger.annotations.ApiModelProperty;
import org.springframework.util.ObjectUtils;

import java.io.Serializable;
import java.util.Optional;

public class ER<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty( value = "状态码", required = true )
    private int code2;

    @ApiModelProperty(value = "是否成功",required = true)
    private boolean success;

    @ApiModelProperty("承载数据")
    private T data;

    @ApiModelProperty(value = "返回消息",required = true )
    private String msg;

    private ER(IResultCode resultCode) {
        this(resultCode, null, resultCode.getMessage());
    }

    private ER(IResultCode resultCode, String msg) {
        this(resultCode, null, msg);
    }

    private ER(IResultCode resultCode, T data) {
        this(resultCode, data, resultCode.getMessage());
    }

    private ER(IResultCode resultCode, T data, String msg) {
        this(resultCode.getCode(), data, msg);
    }

    private ER(int code2, T data, String msg) {
        this.code2 = code2;
        this.data = data;
        this.msg = msg;
        this.success = ResultCode.SUCCESS.code == code2;
    }

    public static boolean isSuccess(ER<?> result) {
        return (Boolean) Optional.ofNullable(result).map((x) -> {
            return ObjectUtils.nullSafeEquals(ResultCode.SUCCESS.code, x.code2);
        }).orElse(Boolean.FALSE);
    }

    public static boolean isNotSuccess(ER<?> result) {
        return !isSuccess(result);
    }

    public static <T> ER<T> data(T data) {
        return data(data, "操作成功");
    }

    public static <T> ER<T> data(T data, String msg) {
        return data(200, data, msg);
    }

    public static <T> ER<T> data(int code2, T data, String msg) {
        return new ER(code2, data, data == null ? "暂无承载数据" : msg);
    }

    public static <T> ER<T> success(String msg) {
        return new ER(ResultCode.SUCCESS, msg);
    }

    public static <T> ER<T> success(IResultCode resultCode) {
        return new ER(resultCode);
    }

    public static <T> ER<T> success(IResultCode resultCode, String msg) {
        return new ER(resultCode, msg);
    }

    public static <T> ER<T> fail(String msg) {
        return new ER(ResultCode.FAILURE, msg);
    }

    public static <T> ER<T> fail(int code2, String msg) {
        return new ER(code2, (Object) null, msg);
    }

    public static <T> ER<T> fail(IResultCode resultCode) {
        return new ER(resultCode);
    }

    public static <T> ER<T> fail(IResultCode resultCode, String msg) {
        return new ER(resultCode, msg);
    }

    public static <T> ER<T> status(boolean flag) {
        return flag ? success("操作成功") : fail("操作失败");
    }

    public int getCode() {
        return this.code2;
    }

    public boolean isSuccess() {
        return this.success;
    }

    public T getData() {
        return this.data;
    }

    public String getMsg() {
        return this.msg;
    }

    public void setCode(final int code2) {
        this.code2 = code2;
    }

    public void setSuccess(final boolean success) {
        this.success = success;
    }

    public void setData(final T data) {
        this.data = data;
    }

    public void setMsg(final String msg) {
        this.msg = msg;
    }

    @Override
    public String toString() {
        return "R(code2=" + this.getCode() + ", success=" + this.isSuccess() + ", data=" + this.getData() + ", msg=" + this.getMsg() + ")";
    }

    public ER() {
    }
}
